deferred class CANVAS_DRAW

inherit
	ANY
	IUP_GET_POINTER

feature {ANY}

	cd_red, cd_dark_red, cd_green, cd_dark_green, cd_blue, cd_dark_blue: INTEGER
	cd_yellow, cd_dark_yellow, cd_magenta, cd_dark_magenta, cd_cyan: INTEGER
	cd_dark_cyan, cd_white, cd_black, cd_dark_gray, cd_gray: INTEGER


feature {NONE}

	cnvs: POINTER

	-- Colors

	initialize_colors
		do
			cd_red := encode_color(255, 0, 0)
			cd_dark_red := encode_color(128, 0, 0)
			cd_green := encode_color(0, 255, 0)
			cd_dark_green := encode_color(0, 128, 0)
			cd_blue := encode_color(0, 0, 255)
			cd_dark_blue := encode_color(0, 0, 128)

			cd_yellow := encode_color(255, 255, 0)
			cd_dark_yellow := encode_color(128, 128, 0)
			cd_magenta := encode_color(255, 0, 255)
			cd_dark_magenta := encode_color(128, 0, 128)
			cd_cyan := encode_color(0, 255, 255)
			cd_dark_cyan := encode_color(0, 128, 128)

			cd_white := encode_color(255, 255, 255)
			cd_black := encode_color(0, 0, 0)

			cd_dark_gray := encode_color(128, 128, 128)
			cd_gray := encode_color(192, 192, 192)
		end

	encode_color (red, green, blue: INTEGER): INTEGER
		require
			red >= 0
			red <= 255
			green >= 0
			green <= 255
			blue >= 0
			blue <= 255
		do
			Result := int_encode_color (red,
												 green,
												 blue)
		end

	encode_color_alpha (red, green, blue, alpha: INTEGER): INTEGER
		require
			red >= 0
			red <= 255
			green >= 0
			green <= 255
			blue >= 0
			blue <= 255
			alpha >= 0
			alpha <= 255
		do
			Result := int_encode_color_alpha (red,
														 green,
														 blue,
														 alpha)
		end

	decode_color (color: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER]
		local
			otr, otg, otb: STRING
			pr, pg, pb: POINTER
			tup: TUPLE[INTEGER, INTEGER, INTEGER]
		do
			int_decode_color (color, pr, pg, pb)

			create otr.make_from_c(pr)
			create otg.make_from_c(pg)
			create otb.make_from_c(pb)

			if otr.is_empty then
				otr.append_string("0")
			end

			if otg.is_empty then
				otg.append_string("0")
			end

			if otb.is_empty then
				otb.append_string("0")
			end

			tup := [otr.at(1).code,
			        otg.at(1).code,
                 otb.at(1).code]
					  
		  Result := tup
		end

	decode_color_alpha (color: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		local
			otr, otg, otb, ota: STRING
			pr, pg, pb, pa: POINTER
			tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		do
			int_decode_color_alpha (color, pr, pg, pb, pa)

			create otr.make_from_c(pr)
			create otg.make_from_c(pg)
			create otb.make_from_c(pb)
			create ota.make_from_c(pa)

			if otr.is_empty then
				otr.append_string("0")
			end

			if otg.is_empty then
				otg.append_string("0")
			end

			if otb.is_empty then
				otb.append_string("0")
			end

			if ota.is_empty then
				ota.append_string("0")
			end

			tup := [otr.at(1).code,
			        otg.at(1).code,
                 otb.at(1).code,
					  ota.at(1).code]
					  
		  Result := tup
		end
	
	-- Internals

	int_encode_color (r, g, b: INTEGER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return cdEncodeColor ($r, $g, $b);"
      end

	int_encode_color_alpha (r, g, b, a: INTEGER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return cdEncodeColorAlpha ($r, $g, $b, $a);"
      end

	int_decode_color (cl: INTEGER; r, g, b: POINTER)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"cdDecodeColor ($cl, $r, $g, $b);"
      end

	int_decode_color_alpha (cl: INTEGER; r, g, b, a: POINTER)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"cdDecodeColorAlpha ($cl, $r, $g, $b, $a);"
      end

end -- Class CANVAS_DRAW

-- The MIT License (MIT)

-- Copyright (c) 2016, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
